package org.polyforms.repository.executor.support;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.Query;

import org.polyforms.repository.query.PaginationProvider;

/**
 * Implementation of find method which returns list of matching entities.
 * 
 * @author Kuisong Tong
 * @since 1.0
 */
@Named
public final class FindExecutor extends QueryExecutor {
    private final PaginationProvider paginationProvider;

    /**
     * Create an instance with {@link PaginationProvider}.
     */
    @Inject
    public FindExecutor(final PaginationProvider paginationProvider) {
        this.paginationProvider = paginationProvider;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Object getResult(final Query query) {
        query.setFirstResult(paginationProvider.getFirstResult());
        query.setMaxResults(paginationProvider.getMaxResults());
        return query.getResultList();
    }

    /**
     * {@inheritDoc}
     */
    public String[] getPrefixes() {
        return new String[] { "find" };
    }
}
